package com.gin.kotlin.no_gay_alliance.adapter.pm

import android.content.Context
import android.content.DialogInterface
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemPrivateMessageUserBinding
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import com.gin.nga.response.user.TinyUserInfo
import retrofit2.Call
import retrofit2.Response

class PrivateMessageUserAdapter(private val messageId: Long, context: Context, handler: MyHandler, data: MutableList<TinyUserInfo>) :
    AbstractBindingRecyclerViewAdapter<ItemPrivateMessageUserBinding, TinyUserInfo>(context, handler, true, data),
    View.OnClickListener, NgaCallback<List<String>> {
    override fun onBindViewHolder(holder: ViewHolder<ItemPrivateMessageUserBinding>, position: Int, b: ItemPrivateMessageUserBinding, dataModel: TinyUserInfo) {
        b.tvUsername.text = dataModel.username

        b.ivDelete.tag = dataModel
        b.ivDelete.setOnClickListener(this)

        b.ivPmBlockAdd.tag = dataModel
        b.ivPmBlockAdd.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        val dataModel = v?.tag as? TinyUserInfo ?: return

        when (v.id) {
            // 踢出用户
            R.id.iv_delete -> AlertDialogUtils.builder(context, "移除用户", listener = { _, which ->
                if (which == DialogInterface.BUTTON_POSITIVE) App.INSTANCE.ngaApi.messageApi.kick(messageId, dataModel.userId!!).enqueue(this)
            }).show()
            // 添加私信黑名单
            R.id.iv_pm_block_add -> {
                AlertDialogUtils.builder(context, "添加 ${dataModel.username} 到黑名单", listener = { _, which ->
                    if (which == DialogInterface.BUTTON_POSITIVE) App.INSTANCE.ngaApi.messageApi.blockAdd(dataModel.userId!!).enqueue(this)
                }).show()
            }
        }


    }

    override fun onSuccess(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, data: List<String>?) {
        data?.get(0)?.also { handler.toast(it) }
    }

    override fun onError(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, errorMessage: String) {
        handler.toastLong(errorMessage)
    }
}